﻿using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FitAutomation.WPF;

namespace FitAutomation_UnitTest
{
	[TestClass]
	public class SmartUpdateCollection
	{
		public SmartUpdateCollection()
		{
			_sut = new SmartUpdateCollection<int>(() => _source);
			_sut.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_sut_CollectionChanged);
		}

		void _sut_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			Trace.WriteLine(e.Action + ": " + e.OldStartingIndex + " " + e.NewStartingIndex);
		}

		List<int> _source = new List<int> { 1, 2, 3, 4 };

		readonly SmartUpdateCollection<int> _sut;

		[TestMethod]
		public void Should_update_adding()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source.Add(5);
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}

		[TestMethod]
		public void Should_update_insering()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source.Insert(2, 77);
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}

		[TestMethod]
		public void Should_update_removing()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source.RemoveAt(2);
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}

		[TestMethod]
		public void Should_update_reseting()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source = new List<int> { 11, 22 };
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}

		[TestMethod]
		public void Should_update_reseting2()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source = new List<int> { 1, 2, 3, 5 };
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}

		[TestMethod]
		public void Should_update_reseting3()
		{
			CollectionAssert.AreEqual(_source, _sut);
			_source = new List<int> { 0, 2, 3, 4 };
			_sut.Update();
			CollectionAssert.AreEqual(_source, _sut);
		}
	}
}
